#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QPushButton>
#include <QMouseEvent>
#include <QWidget>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("2D paint");
    backset();
    broad();
    pathline();
    btclose=new QPushButton(this);
    btclose->setText("close");
    connect(btclose, SIGNAL(clicked(bool)),this, SLOT(close()));
}

void MainWindow::backset()
{
    tweight=700;
    theight=500;
    image = QImage(tweight,theight,QImage::Format_RGB32);
    QColor backColor = qRgb(0,255,255);
    image.fill(backColor);
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawImage(0,0,image);
}

void MainWindow::pathline()
{
    QPainter painter(&image);
    painter.setRenderHint(QPainter::Antialiasing,true);
    QPen pen(Qt::black,3,Qt::SolidLine,Qt::SquareCap,Qt::RoundJoin);
    painter.setPen(pen);
    //路径绘制
    QRect rect(100,100,150,150);
    int startangle1=16*90;
    int endangle1=16*180;
    painter.drawArc(rect,startangle1,endangle1);

    int wight=150;
    int endx=100+150/2;
    int endy=100;
    painter.drawLine(endx,endy,endx+wight,endy);
    endy=100+150;
    painter.drawLine(endx,endy,endx+wight,endy);

    QRect rect2(endx+wight-150/2,endy-150,150,150);
    int startangle2=16*270;
    int endangle2=16*180;
    painter.drawArc(rect2,startangle2,endangle2);
}

void MainWindow::broad()
{
    QPainter painter(&image);

    painter.setRenderHint(QPainter::Antialiasing, true);
    int pointx=80;  //初始位置
    int pointy=80;
    int width=400;
    int height=250;
    painter.setBrush(Qt::white);
    painter.drawRect(50,50,500,300);
    painter.drawLine(pointx,pointy,width+pointx,pointy);
    painter.drawLine(pointx,pointy+height,pointx+width,pointy+height);
    painter.drawLine(pointx,pointy,pointx,pointy+height);
    painter.drawLine(pointx+width,pointy,pointx+width,pointy+height);
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    last=event->globalPos();
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    int dx=event->globalX()-last.x();
    int dy=event->globalY()-last.y();
    last=event->globalPos();
    move(x()+dx,y()+dy);
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    int dx=event->globalX()-last.x();
    int dy=event->globalY()-last.y();
    move(x()+dx,y()+dy);
}

void MainWindow::wheelEvent(QWheelEvent *e)
{
    int wheelpos;
    wheelpos=e->delta();
    if(wheelpos>0)
    {

    }
    else
        if(wheelpos<0)
        {

        }
}



MainWindow::~MainWindow()
{
    delete ui;
}
